package com.iweb.servlet;

import com.iweb.constant.MyConstant;
import com.iweb.constant.ProductAction;
import com.iweb.entity.Product;
import com.iweb.service.ProductService;
import com.iweb.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jay
 * @date 2021/9/13
 * @description
 */
@WebServlet(name = "productServlet", urlPatterns = {"/product.do"})
public class ProductServlet extends HttpServlet {

    private ProductService productService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (ProductAction.ADD.equals(action)) {
            // 打开新增界面
            add(req, resp);
            return;
        }
        if (ProductAction.DOADD.equals(action)) {
            // 处理新增操作
            doAdd(req, resp);
            return;
        }
    }

    // 处理新增操作
    private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Product product = new Product();
            product.setFileName(req.getParameter("fileName"));
            Integer typeOne = req.getParameter("typeOne") == null ? 0 : Integer.valueOf(req.getParameter("typeOne"));
            product.setCategoryLevel1Id(Integer.valueOf(typeOne));
            Integer typeTwo = req.getParameter("typeTwo") == null ? 0 : Integer.valueOf(req.getParameter("typeTwo"));
            product.setCategoryLevel2Id(Integer.valueOf(typeTwo));
            Integer typeThree = req.getParameter("typeThree") == null ? 0 : Integer.valueOf(req.getParameter("typeThree"));
            product.setCategoryLevel3Id(Integer.valueOf(typeThree));
            product.setName(req.getParameter("name"));
            product.setDescription(req.getParameter("description"));
            product.setPrice(Double.valueOf(req.getParameter("price")));
            product.setStock(Integer.valueOf(req.getParameter("stock")));
            product.setIsDelete(0);

            // 保存数据
            productService.add(product);

            // 重定向到首页
            resp.sendRedirect(req.getContextPath() + "/login.do?action=index");

        } catch (Exception e) {
            e.printStackTrace();
            // 设置错误信息
            req.setAttribute("errorMessage", e.getMessage());
            // 跳转到美化后的错误处理界面
            req.getRequestDispatcher(MyConstant.JSPPREFIX + "500.jsp").forward(req, resp);
        }
    }

    // 打开新增界面
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 打开新增界面
        req.getRequestDispatcher(MyConstant.JSPPREFIX + "admin/productadd.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
